home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15468 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: newshost.gu.edu.au!usenet
  2. From: Student (Student)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help rand !
  5. Date: 19 Apr 1996 06:27:09 GMT
  6. Organization: Griffith University
  7. Message-ID: <4l7bnt$r44@griffin.itc.gu.edu.au>
  8. References: <4l5d75$mes@usenetp1.news.prodigy.com> <4l5fj4$1ehs@serra.unipi.it> <4l5vjm$5lj@ccshst05.uoguelph.ca>
  9. NNTP-Posting-Host: enslab29.student.gu.edu.au
  10. X-Newsreader: WinVN 0.92.5
  11.  
  12. In article <4l5vjm$5lj@ccshst05.uoguelph.ca>, thay@uoguelph.ca (Toby K Hay) says:
  13. >
  14. >Maurizio Loreti (loreti@mxsld2.pd.infn.it) wrote:
  15. >: In article <4l5d75$mes@usenetp1.news.prodigy.com>, UNCK19A@prodigy.com (Thomas Furka) writes:
  16. >: >I AM WRITING A PROGRAM FOR SCHOOL THAT PICKS 12 RANDOM NUMBERS. COULD 
  17. >: >SOME ONE PLEASE HELP ME SO THAT I DON'T GET ANY DUPLICATES. I AM PICKING 
  18. >: >FROM THE COMPUTER CLOCK(<TIME.H> USING SRAND.
  19. >
  20. >: Don't shout; we are not blind.
  21. whoever said that last bit is cool!
  22.  
  23. but what you want to know is:
  24.     There is a really excellent algorithm with source
  25.     for generating random numbers with no repeats
  26.     available from Abe's Demo School
  27.     http://www.mds.mdh.se/~dat94avi/demoschool.htm
  28.     It is in his digital effekt.
  29.     The routine is very much faster than simply
  30.     disgarding unwanted results which can get realll sllooow
  31.     near the end of a list if the length is long.
  32. However the source is beyond the scope of a school assignment.
  33. I assume you want something that does this:
  34. 10 random -> 9,3,5,1,7,8,2,4,10,6
  35. You could consider creating an array n[n]
  36. where n represents the highest number
  37. and then just randomly swaping the contents for every n.
  38. ie: before:    n[1] = 1
  39.         n[2] = 2
  40.           :    :
  41.         n[10]=10
  42.  
  43. swap n[1] with n[rand]
  44. swap n[2] with n[rand]
  45. ......................
  46. swap n[10] with n[rand]
  47.     
  48. after:        n[1] = 9
  49.         n[2] = 3
  50.           :    :
  51.         n[10]= 6
  52.  
  53. This ain't that great, but it will work predictably.
  54.